<template>
  <a-modal v-model:open="visible" title="添加工艺环节" destroyOnClose @ok="handle">
    <a-form :model="addFormState" layout="vertical">
      <a-form-item label="工艺名称" name="StepName">
        <a-input v-model:value="addFormState.stepName" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="父环节名称" name="parentId">
        <a-input v-model:value="parentName1" placeholder="请输入" :disabled="true" />
      </a-form-item>
      <a-form-item label="能源类型" name="EnergyTypes">
        <a-select
          v-model:value="addFormState.energyTypesId"
          allowClear
          placeholder="请选择"
        >
          <a-select-option v-for="i in options" :value="i.energyTypesId">
            {{ i.energyName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="原材料" name="RawMaterialTypes">
        <a-select
          v-model:value="addFormState.rawMaterialTypesId"
          allowClear
          placeholder="请选择"
        >
          <a-select-option v-for="i in rawoption" :value="i.rawMaterialTypesId">
            {{ i.materialName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="原辅料" name="AuxiliaryMaterialTypes">
        <a-select
          v-model:value="addFormState.auxiliaryMaterialTypesId"
          allowClear
          placeholder="请选择"
        >
          <a-select-option v-for="i in accessoption" :value="i.auxiliaryMaterialTypesId">
            {{ i.auxiliaryMaterialName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="包装物" name="PackagingTypes">
        <a-select
          v-model:value="addFormState.packagingTypesId"
          allowClear
          placeholder="请选择"
        >
          <a-select-option v-for="i in packageoption" :value="i.packagingTypesId">
            {{ i.packagingName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="活动类别" name="activityCategoriesId">
        <a-select
          v-model:value="addFormState.activityCategoriesId"
          allowClear
          placeholder="请选择"
        >
          <a-select-option v-for="i in activeleibie" :value="i.activityCategoriesId">
            {{ i.activityCategoriesName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="活动类型" name="activityTypesId">
        <a-select
          v-model:value="addFormState.activityTypesId"
          allowClear
          placeholder="请选择"
        >
          <a-select-option v-for="i in activeleixing" :value="i.activityTypesId">
            {{ i.activityTypesName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="排放源" name="sourceId">
        <a-select v-model:value="addFormState.sourceId" allowClear placeholder="请选择">
          <a-select-option v-for="i in source" :value="i.sourceId">
            {{ i.sourceName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="备注" name="Remarks">
        <a-input v-model:value="addFormState.remarks" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";
import { craft } from "/@/api/Carbon module/Carbon accounting module/Carbon monitoring/craft";
import { selfSupportApi } from "/@/api/Carbon module/Carbon accounting module/Carbon monitoring/self-support";
import { upstream } from "/@/api/Carbon module/Carbon accounting module/Carbon monitoring/upstream";
import { energy } from "/@/api/type/Energy";
import { raw } from "/@/api/type/Raw";
import { acessory } from "/@/api/type/accessory";
import { packag } from "/@/api/type/package";
defineExpose({
  showModal,
});
const formRef = ref();

const options = ref([]);
const rawoption = ref([]);
const accessoption = ref([]);
const packageoption = ref([]);
const activeleibie = ref([]);
const activeleixing = ref([]);
const source = ref([]);
const emits = defineEmits(["refresh"]);
const defaultAddForm = {
  stepName: "",
  parentId: "",
  energyTypesId: "",
  rawMaterialTypesId: "",
  auxiliaryMaterialTypesId: "",
  packagingTypesId: "",
  remarks: "",
  activityCategoriesId: "",
  activityTypesId: "",
  sourceId: "",
};

let addFormState = reactive({
  ...defaultAddForm,
});
const queryFormState = reactive({
  pageNum: 1,
  pageSize: 100,
});
const queryForm = reactive({ ...queryFormState });
async function querydata() {
  try {
    let result = await energy.query(queryForm);
    let rawresult = await raw.query(queryForm);
    let accessresult = await acessory.query(queryForm);
    let packageresult = await packag.query(queryForm);
    let activeleibie1 = await upstream.queryactivityCategories(queryForm);
    let activeleixing1 = await upstream.queryactivityTypes(queryForm);
    let sourceresult = await selfSupportApi.queryData(queryForm);
    rawoption.value = rawresult.data.list;
    options.value = result.data.list;
    accessoption.value = accessresult.data.list;
    packageoption.value = packageresult.data.list;
    activeleibie.value = activeleibie1.data.list;
    activeleixing.value = activeleixing1.data.list;
    source.value = sourceresult.data.list;
    console.log(options.value);
  } catch (error) {
    console.log(error);
  }
}
const visible = ref(false);
const parentName1 = ref(null);
function showModal(parentStepId, parentName) {
  visible.value = true;
  addFormState.parentId = parentStepId; // 设置父节点ID
  parentName1.value = parentName;
}
const handle = async () => {
  console.log(addFormState);
  try {
    await craft.Add(addFormState);
    visible.value = false;
    emits("refresh");
  } catch (error) {
    console.log(error);
  }
};

onMounted(() => {
  querydata();
});
</script>
